package fragment;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.projectmoudle_two.R;

import java.util.ArrayList;
import java.util.List;

import view.FlyBorderView;

/**
 * Created by eyesee on 2017/5/13.
 */

public class ListviewFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnKeyListener, AdapterView.OnItemSelectedListener {

    private static final String TAG = "eyesee";

    private View mView;
    private List<String> mList;
    private ListView mLv;
    private FlyBorderView mBorder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(mContext, R.layout.fragment_listview, null);

        mList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            mList.add(i + "号");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(mView);
        setListener();

        return mView;
    }

    private void setListener() {
        mLv.setOnItemClickListener(this);
        mLv.setOnKeyListener(this);
        mLv.setOnItemSelectedListener(this);
    }

    private void initView(View view) {
        mBorder = (FlyBorderView) view.findViewById(R.id.border);
        mBorder.setTvScreen(false);
        mLv = ((ListView) view.findViewById(R.id.lv));


        mLv.setAdapter(new listAdapter(mList));
    }

    public void runScaleAnimator(View view, String animStyle, long duration, float pivot) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, animStyle, 1, 1.2f);
        animator.setDuration(duration);
        view.setPivotX(pivot);
        animator.start();
    }

    public void cancelScaleAnimator(View view, String animStyle, long duration, float pivot) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, animStyle, 1.2f, 1);
        animator.setDuration(duration);
        view.setPivotX(pivot);
        animator.start();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /**这里的view是个布局,即点击的是item的整个布局*/
        runScaleAnimator(mBorder, "scaleX", 500, 400);
        Toast.makeText(mContext, "点击了=="+view, Toast.LENGTH_SHORT).show();
    }

    Handler mHandler = new Handler();
    private boolean isRun = false;
    @Override
    public boolean onKey(View v, int code, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
                //重复一次
                if (event.getRepeatCount() == 1) {                              //得到重复次数
                    mHandler.postDelayed(new Runnable() {                       //延时执行动画
                        @Override
                        public void run() {
                            Log.i(TAG, "onKeyDown++++++======");
                            runScaleAnimator(mBorder, "scaleX", 2000, 400);     //执行拉伸动画
                            isRun = true;                                       //给布尔值重新赋值为true,记录拉伸动画是否执行了
                        }
                    }, 2000);
                }
            }
        }

        if (event.getAction() == KeyEvent.ACTION_UP) {

            if (isRun) {                                                        //判断拉伸动画是否执行了
                cancelScaleAnimator(mBorder, "scaleX", 2000, 400);              //执行缩短动画
            }
            isRun = false;                                                      //将布尔值重置为false
        }
        return false;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int index = mLv.getLastVisiblePosition() - 1;
        mBorder.setVisibility(View.VISIBLE);
        /**满足条件时,只会有一个平移动画执行,到达预定位置处时,不执行距离上的平移*/
        if (position >= index) {
            if (position == index) {
                if (position == parent.getCount()-2){
                    mBorder.setSelectView(view);
                }
                 mLv.smoothScrollToPositionFromTop(position, 100 * 9, 10);
                 ObjectAnimator animator = ObjectAnimator.ofFloat(mBorder, "translationY", 900, 900);
                 animator.setDuration(20);
                 animator.start();
            } else {   //最后一个item被选中时
                mBorder.setSelectView(view);
            }
        } else {
            mBorder.setSelectView(view);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }




    /**adapter设置*/
    private class listAdapter extends BaseAdapter {

        private List<String> mList;

        public listAdapter(List<String> list) {
            this.mList = list;
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(mContext, R.layout.list_item, null);
                holder.mTextView = (TextView) convertView.findViewById(R.id.tv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.mTextView.setText(mList.get(position));

            return convertView;
        }
    }

    class ViewHolder {
        TextView mTextView;
    }
}
